package com.jhgsys.internal.base.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.jhgsys.internal.base.entity.BaseGoodsInventoryRecord;
import com.jhgsys.internal.base.entity.BaseUserScoreRecord;
import com.jhgsys.internal.base.mapper.BaseUserScoreRecordMapper;
import com.jhgsys.internal.base.service.IBaseUserScoreRecordService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.system.entity.User;
import com.jhgsys.internal.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 用户积分记录 Service实现
 *
 * @author admin
 * @date 2024-09-04 13:07:28
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class BaseUserScoreRecordServiceImpl extends ServiceImpl<BaseUserScoreRecordMapper, BaseUserScoreRecord> implements IBaseUserScoreRecordService {

    @Autowired
    private IUserService userService;

    /**
     * 查询（分页）
     * @author admin
     * @date 2024-09-04 13:07:28
     * @param request QueryRequest
     * @param baseUserScoreRecord baseUserScoreRecord
     * @return IPage<BaseUserScoreRecord>
    */
    @Override
    public IPage<BaseUserScoreRecord> findBaseUserScoreRecordPage(QueryRequest request, BaseUserScoreRecord baseUserScoreRecord) {
        LambdaQueryWrapper<BaseUserScoreRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseUserScoreRecord::getDeletedFlg, 0);
        queryWrapper.eq(BaseUserScoreRecord::getUserId, baseUserScoreRecord.getUserId());
        // TODO 设置查询条件
        if(StringUtils.isNotBlank(baseUserScoreRecord.getTenantId())){
            queryWrapper.eq(BaseUserScoreRecord::getTenantId, baseUserScoreRecord.getTenantId());
        }
        if(StringUtils.isNotBlank(baseUserScoreRecord.getOperateType())){
            queryWrapper.eq(BaseUserScoreRecord::getOperateType, baseUserScoreRecord.getOperateType());
        }
        if(null != baseUserScoreRecord.getOperateDate()){
            queryWrapper.eq(BaseUserScoreRecord::getOperateDate, baseUserScoreRecord.getOperateDate());
        }
        queryWrapper.orderByDesc(BaseUserScoreRecord::getCreatedTime);

        Page<BaseUserScoreRecord> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author admin
     * @date 2024-09-04 13:07:28
     * @param baseUserScoreRecord baseUserScoreRecord
     * @return List<BaseUserScoreRecord>
     */
    @Override
    public List<BaseUserScoreRecord> findBaseUserScoreRecordList(BaseUserScoreRecord baseUserScoreRecord) {
	    LambdaQueryWrapper<BaseUserScoreRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseUserScoreRecord::getDeletedFlg, 0);
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author admin
    * @date 2024-09-04 13:07:28
    * @param id
    * @return BaseUserScoreRecord
    */
    @Override
    public BaseUserScoreRecord findBaseUserScoreRecordById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author admin
     * @date 2024-09-04 13:07:28
     * @param baseUserScoreRecord baseUserScoreRecord
     */
    @Override
    @Transactional
    public void createBaseUserScoreRecord(BaseUserScoreRecord baseUserScoreRecord) {
        baseUserScoreRecord.setCreatedTime(new Date());
        this.save(baseUserScoreRecord);
    }

    /**
     * 修改
     * @author admin
     * @date 2024-09-04 13:07:28
     * @param baseUserScoreRecord baseUserScoreRecord
     */
    @Override
    @Transactional
    public void updateBaseUserScoreRecord(BaseUserScoreRecord baseUserScoreRecord) {
        baseUserScoreRecord.setUpdatedTime(new Date());
        this.saveOrUpdate(baseUserScoreRecord);
    }

    /**
     * 逻辑删除
     * @author admin
     * @date 2024-09-04 13:07:28
     * @param baseUserScoreRecord baseUserScoreRecord
     * @param baseUserScoreRecordIds baseUserScoreRecordIds
     */
    @Override
    @Transactional
    public void deleteBaseUserScoreRecord(BaseUserScoreRecord baseUserScoreRecord,String baseUserScoreRecordIds) {
        List<String> list = Arrays.asList(baseUserScoreRecordIds.split(StringPool.COMMA));
        this.baseMapper.update(baseUserScoreRecord,new LambdaQueryWrapper<BaseUserScoreRecord>().in(BaseUserScoreRecord::getUserScoreRecordId,list));
	}

    /**
    * 物理删除
    * @author admin
    * @date 2024-09-04 13:07:28
    * @param baseUserScoreRecordIds baseUserScoreRecordIds
    */
    @Override
    @Transactional
    public void physicallyDeleteBaseUserScoreRecord(String baseUserScoreRecordIds) {
        List<String> list = Arrays.asList(baseUserScoreRecordIds.split(StringPool.COMMA));
        LambdaQueryWrapper<BaseUserScoreRecord> wapper = new LambdaQueryWrapper<>();
        wapper.in(BaseUserScoreRecord::getUserScoreRecordId,list);
        this.remove(wapper);
    }

    /**
     * 获取用户当前积分
     * @param userId
     * @return
     */
    @Override
    public int findTotalScoreByUserId(String userId) {
        User user = userService.findById(userId);
        Integer totalScore = user.getTotalScore();
        if (null == totalScore) {
            totalScore = 0;
        }
        return totalScore;
    }
}
